<template>
  <div id="subpage" v-loading="loading">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <b>网站设置</b>
        </el-breadcrumb-item>
        <el-breadcrumb-item>数据字典设置</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <div class="content">
      <div class="xcx-head">
        <span class="title">数据字典设置</span>
      </div>
      <div class="xcx-content">
        <el-tabs @tab-click="tabIndex" :tab-position="tabPosition" style="height: 720px;">
          <el-tab-pane v-for="(item, index) in tableData" :key="index" :label="item.dkey + '(' + item.dictname + ')'">
            <div class="xcx-head">
              <div style="display: flex">
                <span>字典内容</span>
              </div>
              <div style="display: flex">
                <el-button @click="dialogFormVisible = true" icon="el-icon-plus" size="small"
                  type="primary">新建</el-button>
              </div>
            </div>
            <div class="xcx-content">
              <!--列表-->
              <el-table border :data="item.children" style="width: 100%" row-key="id" size="mini" v-loading="loading">
                <el-table-column align="center" prop="id" label="序号" width="80"></el-table-column>
                <el-table-column prop="dkey" label="名称" width="*"></el-table-column>
                <el-table-column align="center" prop="dvalue" label="字典值" width="120"></el-table-column>
                <el-table-column align="center" prop="sort" label="排序" width="120"></el-table-column>
                <el-table-column align="center" label="操作" width="180">
                  <template #default="scope">
                    <el-button plain icon="el-icon-edit" size="mini" type="primary"
                      @click="edit(scope.row.id)">编辑</el-button>
                    <el-button plain icon="el-icon-delete" size="mini" type="danger"
                      @click="del_item(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!--分页-->
              <div class="paging">
                <el-pagination class="left" :page-size="limit" @current-change="handleCurrentChange" :current-page="page"
                  background layout="total, prev, pager, next" :total="count"></el-pagination>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

    <el-dialog title="分类操作" width="36%" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
      @close="closeDialog">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="名称：" :label-width="formLabelWidth" prop="dkey">
          <el-input v-model="ruleForm.dkey" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典值：" :label-width="formLabelWidth" prop="dvalue">
          <el-input v-model="ruleForm.dvalue" :disabled="syncDisable" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序：" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      syncDisable: false,
      tabPosition: 'left',
      tableData: [],
      page: 0,
      count: 0,
      limit: 12,
      formLabelWidth: "120px",
      ruleForm: {
        id: 0,
        did: 0,
        dkey: "",
        dvalue: "",
        dictname: "",
        sort: 0,
      },
      rules: {
        dkey: [{ required: true, message: "请填写名称", trigger: "blur" }],
        dvalue: [{ required: true, message: "请填写值，且不能与已有的值重复", trigger: "blur" }],
      },
    };
  },
  mounted() {
    var that = this;
    that.getList();
    //that.getDetail();
  },
  methods: {

    tabIndex(e) {
      this.ruleForm.did = this.tableData[e.index].id;
      this.ruleForm.dictname = this.tableData[e.index].dictname;
    },

    del_item(ids) {
      console.log(ids);
      let that = this;
      this.$confirm("此操作不可逆, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        that.axios
          .post("/dict/destroy", {
            ids: ids,
          })
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              that.getList();
            } else {
              this.$message({
                type: "info",
                message: res.msg,
              });
            }
          });
      });
    },

    edit(ids) {
      var that = this;
      that.syncDisable = true;
      that.ruleForm.id = ids;
      that.axios.post("/dict/detail", { id: ids }).then((res) => {
        if (res) {
          that.ruleForm.id = res.data.id;
          that.ruleForm.dkey = res.data.dkey;
          that.ruleForm.dvalue = res.data.dvalue;
          that.ruleForm.sort = res.data.sort;
          that.dialogFormVisible = true;
        }
      });
    },

    closeDialog() {
      const that = this;
      that.syncDisable = false;
      that.ruleForm.sort = 0;
      that.ruleForm.dkey = "";
      that.ruleForm.dvalue = "";
      that.ruleForm.dictname = "";
      that.ruleForm.did = 0;
    },

    handleCurrentChange(currentPage) {
      var that = this;
      that.page = currentPage;
      that.getList();
    },

    /**
     * 获取列表
     */
    getList() {
      let that = this;
      let formData = {};
      var curr_page = sessionStorage.getItem("curr_page");
      that.page = curr_page ? curr_page - 0 : that.page;
      formData.page = that.page;
      formData.did = that.did;
      formData.limit = that.limit;
      that.axios.post("/dict/index", formData).then((res) => {
        console.log(res);
        if (res.code == 200) {
          that.ruleForm.did = res.data.data[0].id;
          that.tableData = res.data.data;
          that.count = res.data.total;
          that.page = curr_page ? curr_page : that.page;
          curr_page ? sessionStorage.removeItem("curr_page") : "";
          that.loading = false;
        }
      });
    },

    submitForm(formName) {
      const that = this;
      that.$refs[formName].validate((valid) => {
        if (!valid) return that.$message.warning("请完整填写内容!");
        var formData = {};
        formData = that.ruleForm;
        var url = "/dict/beforeSave";
        that.axios.post(url, formData).then(() => {
          that.$message.success(that.id ? "修改成功" : "添加成功");
          that.dialogFormVisible = false;
          that.syncDisable = false;
          that.ruleForm.sort = 0;
          that.ruleForm.dkey = "";
          that.ruleForm.dvalue = "";
          that.ruleForm.dictname = "";
          that.ruleForm.did = 0;
          that.getList();
        });
      });
    },

  },
};
</script>
